<template>
    <el-card>
        <div slot="header" class="clearfix">
            <h3>新增课程</h3>
        </div>
        <!-- 新增内容框 -->
        <el-form :model="addSubjectData" label-width="100px">
            <el-form-item label="选择专业：">
                <el-select v-model="addSubjectData.subjectsId" placeholder="请选择专业">
                    <el-option v-for="item in subjectsOptionData" :key="item._id" :label="item.name" :value="item._id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="课程名称：">
                <el-input v-model="addSubjectData.name"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addOneCourseSubmit(addSubjectData)">确认新增</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
    import api from "../../../http/modules/api";

    import {
        createNamespacedHelpers
    } from "vuex";
    // 引入课程辅助函数
    const {
        mapActions,
    } = createNamespacedHelpers(
        "courses"
    );
    export default {
        data() {
            return {
                addSubjectData: {
                    name: "",
                    subjectsId: "",
                },
                subjectsOptionData: [],
            };
        },
        mounted() {
            this.getSubjectOptionData();
        },
        methods: {
            ...mapActions(['addOneCourse']),
            // 获取专业列表数据
            async getSubjectOptionData() {
                const res = await api.subjects.getSubjects();
                if (res.code) {
                    this.subjectsOptionData = res.data.result
                }
            },
            // 新增一个学生
            async addOneCourseSubmit(addData) {
                const res = await this.addOneCourse(addData);
                if (res.code) {
                    this.$message({
                        message: '新增成功！',
                        type: 'success',
                        center: true,
                    });
                } else {
                    this.$message({
                        message: '新增失败！',
                        type: 'error',
                        center: true,
                    });
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .el-card {

        .clearfix:before,
        .clearfix:after {
            display: table;
            content: "";
        }

        .clearfix:after {
            clear: both
        }

        .el-form {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            .el-form-item {
                width: 400px;

                .el-button {
                    margin-top: 30px
                }

                ;

                .avatar-uploader {
                    width: 100px;
                    border: 1px dashed #666;
                    border-radius: 6px;
                    cursor: pointer;
                    position: relative;
                    overflow: hidden;
                }

                .avatar-uploader:hover {
                    border-color: #409EFF;
                }

                .avatar-uploader-icon {
                    font-size: 28px;
                    color: #8c939d;
                    width: 100px;
                    height: 100px;
                    line-height: 100px;
                    text-align: center;
                }

                .avatar {
                    width: 100px;
                    height: 100px;
                    display: block;
                }
            }
        }
    }
</style>